<?php

namespace App\Http\Controllers\Auth;

use App\Models\Admin;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    protected $redirectPath = '/';

    protected $username = 'username';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'username' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'username' => $data['username'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

    public function postRegister(){
        $data['username']=\Input::get('username');
        $data['email']=\Input::get('email');
        $data['phone']=\Input::get('phone');
        $data['password']=bcrypt(\Input::get('password'));
        $data['fullname']=\Input::get('fullname');
        $data['role_id']=4;
        $data['status_at']=1;
        $isExist=Admin::where('username',$data['username'])->first();
        if($isExist){
             return redirect('tooltip/failure')->with(['redirectway'=>'用户名已存在，注册失败！','redirecturl'=>'/auth/login']);
        }
        $isExist=Admin::where('email',$data['email'])->first();
        if($isExist){
             return redirect('tooltip/failure')->with(['redirectway'=>'邮箱已存在，注册失败！','redirecturl'=>'/auth/login']);
        }
        $isExist=Admin::where('phone',$data['phone'])->first();
        if($isExist){
             return redirect('tooltip/failure')->with(['redirectway'=>'手机已存在，注册失败！','redirecturl'=>'/auth/login']);
        }
        $res=$admin = Admin::create($data);
        if($res){
            return redirect('tooltip/success')->with(['redirectway'=>'注册成功！','redirecturl'=>'/auth/login']);
        }else{
            return redirect('tooltip/failure')->with(['redirectway'=>'注册失败！','redirecturl'=>'/auth/retister']);
        } 
    }

    public function postLogin(){

        // dd(md5("jiqin"));
        $request=\Input::get();
        $isauth=\Auth::attempt(['username' => $request['username'], 'password' => $request['password'], 'status_at' => 1]);
        if ($isauth)
        {
            return redirect()->intended('/');
        }
        return view('auth/login');
    }
}
